import { Status } from '@libs/common';
import { BelongsToMany, Column, DataType, Model, Table } from 'sequelize-typescript';
import SysRole from './sys.role.entity';
import SysUserRole from './user.role.entity';

@Table({
    underscored: true,
    comment: '用户表',
    tableName: 'sys_user',
    paranoid: true,
    freezeTableName: true
})
class SysUser extends Model<SysUser> {
    @Column({
        unique: true,
        primaryKey: true,
        autoIncrement: true,
        allowNull: false,
        comment: '用户id'
    })
    id: number;

    @Column({
        type: DataType.CHAR(255),
        allowNull: true,
        comment: '用户昵称'
    })
    nickName:string

    @Column({
        allowNull: false,
        comment: '用户所属平台'
    })
    platform: number

    @Column({
        type: DataType.CHAR(255),
        unique: true,
        allowNull: false,
        comment: '用户登录账号'
    })
    account: string

    @Column({
        type: DataType.CHAR(255),
        allowNull: false,
        comment: '登录密码'
    })
    password: string

    @Column({
        type: DataType.TEXT,
        allowNull: true,
        comment: '用户头像'
    })
    avatar: string

    @Column({
        allowNull: false,
        comment: '用户状态0 表示禁用 1 表示启用',
        defaultValue: Status.ENABLE,
    })
    status: number

    @Column({
        type: DataType.CHAR(255),
        allowNull: true,
        comment: '邮箱',
        validate:{
            isEmail: { msg: '请输入正确的邮箱格式' }
        }
    })
    email: string
    
    @BelongsToMany(() => SysRole, () => SysUserRole)
    roles: Array<SysRole>
}

export default SysUser